package com.bibrApp.difu.base;

import android.app.Application;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.utils.AppManager;
import com.bibrApp.difu.utils.KeyboardUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @param <T>
 * @author dj
 * @date 2019/7/22
 */
public abstract class BaseActivity<T extends BasePresenter> extends RxAppCompatActivity {

    protected T presenter;
    protected DfApp app;

    protected abstract void initPresenter();

    protected abstract int getLayoutId();

    protected Unbinder unbinder;

    @Override
    protected void onStart() {
        super.onStart();
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //当app在后台运行，去设置页面关闭权限后,重回应用会杀死该进程,这里重新回到启动页
        super.onCreate(savedInstanceState);

        setContentView(getLayoutId());
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        unbinder = ButterKnife.bind(this);

        app = (DfApp) getApplication();

        initPresenter();
        AppManager.addActivity(this);



        ImmersionBar.with(this)
                .statusBarColor(statusBarColor())
                .statusBarDarkFont(statusBarFont())
                .init();

    }

    @ColorRes
    public int statusBarColor() {
        return R.color.white;
    }

    public boolean statusBarFont() {
        return true;
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        KeyboardUtils.hideSoftInput(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        ImmersionBar.with(this).destroy();
    }

    /**
     * Fragment替换(核心为隐藏当前的,显示现在的,用过的将不会destrory与create)
     */
    private Fragment currentFragment;

    protected void smartReplaceFragment(@IdRes int flId, Fragment toFragment, String tag) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        // 如有当前在使用的->隐藏当前的
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        // toFragment之前添加使用过->显示出来
        if (manager.findFragmentByTag(tag) != null) {
            transaction.show(toFragment);
        } else {// toFragment还没添加使用过->添加上去
            transaction.add(flId, toFragment, tag);
        }
        transaction.commitAllowingStateLoss();
        // toFragment 更新为当前的
        currentFragment = toFragment;
    }

    public void smartReplaceFragment(@IdRes int flId, Fragment toFragment) {
        smartReplaceFragment(flId, toFragment, toFragment.getClass().getSimpleName());
    }


    public void showToast(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        ToastUtils.ShowToast(this, msg);
    }

    public void startActivity(Class<?> cls) {
        startActivity(new Intent(this, cls));
    }

}
